﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using CRMWeb.Utilities.Context;
using Newtonsoft.Json;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for Notice
    /// </summary>
    public class Notice : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {

            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }


        //user defined function
        #region private method


        //Select Notice list and bind to table:
        public void Selectnotice(string Company)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = NoticeHandler.GetNoticeList(pageIndex, pageSize,Company);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        //Add new notice:
        public int AddNotice(string NoticeDescription, string CreateDate, string EditDate, string Operator, string NoticeTitle, string CompanyList)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = NoticeHandler.AddNotice(NoticeDescription, CreateDate, EditDate, Operator, NoticeTitle, CompanyList);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }
        //Edit existing notice:
        public int EditNotice(int NoticeID, string NoticeDescription, string EditDate, string NoticeTitle, string CompanyList)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = NoticeHandler.EditNotice(NoticeID, NoticeDescription, EditDate, NoticeTitle, CompanyList);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        //Delete notice:
        public bool DeleteNotice(int NoticeID)
        {
            bool flag = false;
            try
            {
                flag = NoticeHandler.DeleteNotice(NoticeID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        //Show recent notice:
        public void ShowLatestNotice(string CompanyList)
        {
            try
            {

                CompanyList = context.Request.Params["CompanyList"];
                DataSet ds = NoticeHandler.ShowLatestNotice(CompanyList);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void BindAllRole()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(CmsMemberGroupHandler.GetAllGroup());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }


        public void BindCurrentUserRole()
        {
            int UserId = Convert.ToInt32(CMSContext.CurrentUser.NodeID);
            try
            {
                string strJson = JsonConvert.SerializeObject(CmsMemberGroupHandler.BindAllCurrRole(UserId));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void BindLogoRoles()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(CmsMemberGroupHandler.BindLogoRoles());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }
        #endregion

    }
}